﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace A.HTMlServer
{
    public class ArticleData
    {
        public ArticleData()
        {}

        public static void DatatoSql(HListModel model)
        {

            string sql = "insert into weburl(url,other,code,other2)values('" + model.Url + "','" + model.Other + "','" + model.Code + "','" + model.Name + "')";
            //WF.DbHelperSQL.ExecuteSql(sql);
            DbHelperOleDb.ExecuteSql(sql);
        }

        public static void DatatoUpdateFlag(string moreid)
        {
            string sql = "update weburl set flag=1 where id not in ("+moreid+")";
            DbHelperOleDb.ExecuteSql(sql);
        }
        public static void DatatoSqlArticle(ArticleModel model)
        {

            //string sql = "insert into Article(Title,[Content],[From],Introduction,page,[Date],other,Code,CodeName)"+
                //"values(''" +model.Title + "'',''" + model.Content + "'',''" + model.From + "'',''" + model.Introduction + "'',"+model.page+",#"+model.Date+"#,''"+model.Other+"'',''"+model.Code+"'',''"+model.CodeName+"'')";
            string sql = "insert into Article(Title,[Content],[From],Introduction,page,[Date],other,Code,CodeName)" +
                "values(@Title,@Content,@From,@Introduction,@Page,@Date,@Other,@Code,@CodeName)";
            OleDbParameter[] p =
            {
                new OleDbParameter("@Title",OleDbType.VarChar,300),
                new OleDbParameter("@Content",OleDbType.LongVarChar),
                new OleDbParameter("@From",OleDbType.VarChar,50),
                new OleDbParameter("@Introduction",OleDbType.VarChar,500),
                new OleDbParameter("@Page",OleDbType.Integer),
                new OleDbParameter("@Date",OleDbType.Date),
                new OleDbParameter("@Other",OleDbType.VarChar,50),
                new OleDbParameter("@Code",OleDbType.VarChar,50),
                new OleDbParameter("@CodeName",OleDbType.VarChar,50)
                             };
            p[0].Value = model.Title;
            p[1].Value = model.Content;
            p[2].Value = model.From;
            p[3].Value = model.Introduction;
            p[4].Value = model.page;
            p[5].Value = model.Date;
            p[6].Value ="";
            //p[6].IsNullable = true;
            p[7].Value = model.Code;
            p[8].Value = model.CodeName;

            DbHelperOleDb.ExecuteSql(sql,p);
            DatatoSqlHiArticle(model);
        }

        public static void DatatoSqlHiArticle(ArticleModel model)
        {
            //string sql = "insert into Article(w_Title,[w_Content],[w_From],w_Introductions,w_page,[w_time],w_Code,w_flag)" +
            //    "values(''" + model.Title + "'',''" + model.Content + "'',''" + model.From + "'',''" + model.Introduction + "''," + model.page + ",''" + model.Date + "'',''12'',12)";
            string sql = "insert into Article(w_Title,[w_Content],[w_From],w_Introductions,w_page,[w_time],w_Code,w_flag)" +
                 "values(@Title,@Content,@From,@Introduction,@Page,@Date,@Code,@Flag)";
            SqlParameter[] p =
            {
                new SqlParameter("@Title",SqlDbType.VarChar,300),
                new SqlParameter("@Content",SqlDbType.NText),
                new SqlParameter("@From",SqlDbType.VarChar,50),
                new SqlParameter("@Introduction",SqlDbType.VarChar,500),
                new SqlParameter("@Page",SqlDbType.Int),
                new SqlParameter("@Date",SqlDbType.DateTime),
                new SqlParameter("@Code",SqlDbType.VarChar,50),
                new SqlParameter("@Flag",SqlDbType.TinyInt)
                             };
            //参数化SqlParameter
            //单引号用2个单引号表示

            p[0].Value = model.Title;
            p[1].Value = model.Content;
            p[2].Value = model.From;
            p[3].Value = model.Introduction;
            p[4].Value = model.page;
            p[5].Value = model.Date;
            p[6].Value = 12;
            p[7].Value = 12;

            DbHelperSQL.ExecuteSql(sql,p);
        }

        public static List<HListModel> DataHListModel()
        {
            List<HListModel> List = new List<HListModel>();
            string sql = "select id,url,other,code,other2 from  weburl where url is not null";
            //('" + model.Url + "','" + model.Other + "','" + model.Code + "','" + model.Name + "')";
            //WF.DbHelperSQL.ExecuteSql(sql);
            DataSet ds = A.HTMlServer.DbHelperOleDb.Query(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                HListModel model;
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    model = new HListModel();
                    if (dt.Rows[i]["id"].ToString() != "")
                    {
                        model.ID = int.Parse(dt.Rows[i]["id"].ToString());
                    }
                    model.Url = dt.Rows[i]["url"].ToString();
                    model.Other = dt.Rows[i]["other"].ToString();
                    model.Code = dt.Rows[i]["code"].ToString();
                    model.Name = dt.Rows[i]["other2"].ToString();
                    List.Add(model);
                }
            }
            return List;
        }
    }
}
